package Clases;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.util.ParserException;

public class Indexador {

	public static void main(String[]args){
		StandardAnalyzer analizador = new StandardAnalyzer(Version.LUCENE_30);

//		File indexDir = new File("C:/Users/leo/Desktop/ObliIngInf/PruebaIndice");
//		File directorioArchivos = new File("C:/Users/leo/Desktop/ObliIngInf/wikipedia/wikipedia");

		File indexDir = new File("C:/Users/maureyes/workspace/indice");
		File directorioArchivos = new File("C:/Users/maureyes/workspace/wikipedia");
		
		IndexWriter indexWriter;
		try {
			indexWriter = new IndexWriter(
					FSDirectory.open(indexDir), analizador, true,
					IndexWriter.MaxFieldLength.LIMITED);
			indexWriter.setUseCompoundFile(false);

			indexar(indexWriter, directorioArchivos);

			System.out.println("Se han indexado " + indexWriter.maxDoc() + " documentos");

			indexWriter.optimize();
			indexWriter.close();

		} catch (CorruptIndexException e) {
			System.out.println("Ha ocurrido un error"); 
		} catch (LockObtainFailedException e) {
			System.out.println("Ha ocurrido un error");
		}catch (IOException e) {
			System.out.println("Ha ocurrido un error");
		}
	}

	private static void indexar(IndexWriter indexWriter, File aIndexar) throws FileNotFoundException{
		try{
			if(aIndexar.isDirectory()){
				for (File f : aIndexar.listFiles()) {
					indexar(indexWriter, f);
				}
			}else{
				if(aIndexar.exists() && aIndexar.canRead()){
					
					Parser parser = new Parser();
					try{
						System.out.println(aIndexar.getCanonicalPath());
						parser.setEncoding("utf-8");
						parser.setURL("file:///"+aIndexar.getCanonicalPath());				
					} catch (ParserException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					StringBean sb = new StringBean();
					sb.setURL(parser.getURL());
					Document doc= new Document();		
					
					doc.add(new Field("prueba",new StringReader(sb.getStrings())));
					//doc.add(new Field("contenido",fr));
					doc.add(new Field("nombre", aIndexar.getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));
					indexWriter.addDocument(doc);
					System.out.println("Indexando: " + aIndexar.getCanonicalPath());
				}
			}
		}catch (IOException e) {
			// TODO: handle exception
		}
	}
}